package org.zjkoye.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zjkoye.crm.common.Result;
import org.zjkoye.crm.controller.UserController;
import org.zjkoye.crm.entity.Customer;
import org.zjkoye.crm.mapper.CustomerMapper;
import org.zjkoye.crm.mapper.UsermainMapper;
import org.zjkoye.crm.service.CustomerService;
import org.zjkoye.crm.utils.ThreadLocalUtil;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zjkoye
 * @since 2024-06-23
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private UsermainMapper usermainMapper;

    /**
     * 根据登录名查询用户
     * @param loginName   登录名
     * @return   User
     */
    @Override
    public Customer findByCustomerName(String loginName) {
        LambdaQueryWrapper<Customer> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(Customer::getCustomerName, loginName);
        return customerMapper.selectOne(userWrapper);
    }

    @Override
    public Customer getCustomerById(int id) {
        return customerMapper.selectById(id);
    }

    /**
     * 查询所有用户信息
     * @return List<Customer>
     */
    @Override
    public List<Customer> getUserList() {

        return customerMapper.selectList(null);
    }
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    /**
     *  分页查询用户信息
     * @param pageSize 每页显示的条数
     * @param currentPage  要查询的页
     * @param name  用户姓名
     * @return IPage<Customer>
     */
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public IPage<Customer> pageList(int pageSize, int currentPage, String name) {
        // like 条件构造
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null && !"".equals(name), Customer::getCustomerName, name);

        // 从 ThreadLocal 中获取当前用户


        // 设置地区过滤条件
        Map<String, Object> userSession = ThreadLocalUtil.get();
        String regionName = (String)userSession.get("regionName");
        logger.info(userSession.toString());
        logger.info((String) userSession.get("regionName") + "!!!!!!!!!!!!!!!!");
        int id = (Integer)userSession.get("id");
        logger.info(id + "!!!!!!!!!");
        regionName = usermainMapper.selectById(id).getRegionName();
        wrapper.eq(Customer::getRegionName, regionName);

        // 分页构造
        IPage<Customer> page = new Page<>(currentPage, pageSize);
        customerMapper.selectPage(page, wrapper);
        return page;
    }

    @Override
    public Result<String> add(Customer user) {
        if (user.getCustomerName() == null || "".equals(user.getCustomerName())) {
            return Result.error("客户名不能为空");
        }
        if(user.getAddress() == null || "".equals(user.getAddress())) {
            return Result.error("客户地址不能为空");
        }
        if(user.getLocation() == null || "".equals(user.getLocation())) {
            return Result.error("客户归属地不能为空");
        }

        if (customerMapper.insert(user) > 0) {
            return Result.success("添加成功");
        }

        return Result.error("添加失败");
    }

    @Override
    public boolean updateUser(Customer user) {
        LambdaUpdateWrapper<Customer> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(user.getLocation() != null && !"".equals(user.getLocation()), Customer::getLocation, user.getLocation())
                .eq(Customer::getCustomerId, user.getCustomerId());
        return customerMapper.update(wrapper) > 0;
    }

    @Override
    public boolean deleteById(int id) {
        return customerMapper.deleteById(id) > 0;
    }

}
